<?php

App::uses('AppController', 'Controller');

/**
 * CiedesPacientes Controller
 *
 * @property CiedesPaciente $CiedesPaciente
 */
class CiedesPacientesController extends AppController
{
    public $uses = array('CiedesPaciente', 'Ciede');
    
    public function beforeFilter()
    {
        parent::beforeFilter();
        //$this->Auth->allow();
    }
    
    /**
     * index method
     *
     * @return void
     */
    public function index()
    {
        $this->CiedesPaciente->recursive = 0;
        $this->set('ciedesPacientes', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null)
    {
        if (!$this->CiedesPaciente->exists($id))
        {
            throw new NotFoundException(__('Invalid ciedes paciente'));
        }
        $options = array('conditions' => array('CiedesPaciente.' . $this->CiedesPaciente->primaryKey => $id));
        $this->set('ciedesPaciente', $this->CiedesPaciente->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add()
    {
        if ($this->request->is('post'))
        {
            $this->CiedesPaciente->create();
            if ($this->CiedesPaciente->save($this->request->data))
            {
                $this->flash(__('Ciedespaciente saved.'), array('action' => 'index'));
            } else
            {
                
            }
        }
        $pacientes = $this->CiedesPaciente->Paciente->find('list');
        $consultasexternas = $this->CiedesPaciente->Consultasexterna->find('list');
        $ciedes = $this->CiedesPaciente->Ciede->find('list');
        $this->set(compact('pacientes', 'consultasexternas', 'ciedes'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null)
    {
        if (!$this->CiedesPaciente->exists($id))
        {
            throw new NotFoundException(__('Invalid ciedes paciente'));
        }
        if ($this->request->is('post') || $this->request->is('put'))
        {
            if ($this->CiedesPaciente->save($this->request->data))
            {
                $this->flash(__('The ciedes paciente has been saved.'), array('action' => 'index'));
            } else
            {
                
            }
        } else
        {
            $options = array('conditions' => array('CiedesPaciente.' . $this->CiedesPaciente->primaryKey => $id));
            $this->request->data = $this->CiedesPaciente->find('first', $options);
        }
        $pacientes = $this->CiedesPaciente->Paciente->find('list');
        $consultasexternas = $this->CiedesPaciente->Consultasexterna->find('list');
        $ciedes = $this->CiedesPaciente->Ciede->find('list');
        $this->set(compact('pacientes', 'consultasexternas', 'ciedes'));
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @throws MethodNotAllowedException
     * @param string $id
     * @return void
     */
    public function delete($id = null)
    {
        $this->CiedesPaciente->id = $id;
        if (!$this->CiedesPaciente->exists())
        {
            throw new NotFoundException(__('Invalid ciedes paciente'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->CiedesPaciente->delete())
        {
            $this->flash(__('Ciedes paciente deleted'), array('action' => 'index'));
        }
        $this->flash(__('Ciedes paciente was not deleted'), array('action' => 'index'));
        $this->redirect(array('action' => 'index'));
    }

    public function ajaxregistra($idconsulta=null, $alerta=null)
    {
        
        $this->layout = 'ajax';
        if(!empty($this->request->data)){
        //debug($this->request->data);exit;  
        $frase = $this->request->data['Ciede']['descod4'];
        $idPaciente = $this->request->data['CiedesPaciente']['paciente_id'];
        $idConsulta = $this->request->data['CiedesPaciente']['consultasexterna_id'];
        $observaciones = $this->request->data['CiedesPaciente']['observaciones'];
        //debug($frase);
        $cadena = substr($frase, 0, -1);
        //debug($cadena);        
        $cidies = $this->Ciede->find('first', array(
            'recursive'=>-1,
            'conditions'=>array('Ciede.descod4 like'=>"%$cadena%")
        ));
        //debug($cidies);
        $idCiede = $cidies['Ciede']['id'];
        $this->request->data='';
        $this->request->data['CiedesPaciente']['paciente_id']=$idPaciente;
        $this->request->data['CiedesPaciente']['consultasexterna_id']=$idConsulta;
        $this->request->data['CiedesPaciente']['ciede_id']=$idCiede;
        $this->request->data['CiedesPaciente']['observaciones']=$observaciones;
        //$this->request->data['Ciedespaciente']['paciente_id']=$idPaciente;
        //$cidies['Ciede']['id'];
        //debug($this->request->data);
        $this->CiedesPaciente->create();
        $this->CiedesPaciente->save($this->request->data);
        $cie10Paciente = $this->CiedesPaciente->find('all', array(
            'recursive'=>0,
            'conditions'=>array(
                'CiedesPaciente.paciente_id'=>$idPaciente,
                'CiedesPaciente.consultasexterna_id'=>$idConsulta,
            )
        ));
        //debug($cie10Paciente);exit;
        $this->redirect(array('controller'=>'Pacientes','action'=>'verestadopaciente', $idConsulta, 'txt_diagnostico'));
        }else{
            $this->set(compact('idconsulta', 'alerta'));
        }
        
    }
    public function ajaxcie(){
        $this->layout = 'ajax';        
    }

}